﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using FirstFloor.ModernUI.Presentation;
using Tisbi.LawFirm.DAL.Models;
using Tisbi.LawFirm.UI.Views.Controls;

namespace Tisbi.LawFirm.UI.ViewModel
{
    public class LawsuitCRUDViewModal : BaseCRUDItemViewModel<Lawsuits>
    {
        public RelayCommand SelectCourtTypes { get; private set; }
        public RelayCommand SelectDistricts { get; private set; }
        public RelayCommand SelectPlaintiffs { get; private set; }
        public RelayCommand SelectDefendants { get; private set; }
        public RelayCommand SelectThirdParties { get; private set; }
        public RelayCommand SelectStatementOfClaimTypes { get; private set; }
        public RelayCommand SelectCourts { get; private set; }
        public RelayCommand SelectLocationCaseMaterials { get; private set; }


        public LawsuitCRUDViewModal(Lawsuits model) : base(model)
        {
            SelectCourtTypes = new RelayCommand(SelectCourtTypesDialog);
            SelectDistricts = new RelayCommand(SelectDistrictsDialog);
            SelectPlaintiffs = new RelayCommand(SelectPlaintiffsDialog);
            SelectDefendants = new RelayCommand(SelectDefendantsDialog);
            SelectThirdParties = new RelayCommand(SelectThirdPartiesDialog);
            SelectStatementOfClaimTypes = new RelayCommand(SelectStatementOfClaimTypesDialog);
            SelectCourts = new RelayCommand(SelectCourtsDialog);
            SelectLocationCaseMaterials = new RelayCommand(LocationCaseMaterialsDialog);
        }

        private void LocationCaseMaterialsDialog(object obj)
        {
            Court selectedItem;
            var items = CreateNewDependencyItemDialog<Court, CourtsListView>("Новый суд", out selectedItem);
            LocationCaseMaterials = items;
            SelectedLocationCaseMaterials = selectedItem;
        }

        private void SelectCourtsDialog(object obj)
        {
            Court selectedItem;
            var item = CreateNewDependencyItemDialog<Court, CourtsListView>("Новый суд", out selectedItem);
            Courts = item;
            SelectedCourts = selectedItem;
        }

        private void SelectStatementOfClaimTypesDialog(object obj)
        {
            StatementOfClaimType selectedItem;
            var item = CreateNewDependencyItemDialog<StatementOfClaimType, StatementOfClaimListView>("Новый вид искового заявления", out selectedItem);
            StatementOfClaimTypes = item;
            SelectedStatementOfClaimType = selectedItem;
        }

        private void SelectThirdPartiesDialog(object obj)
        {
            Customers selectedItem;
            var item = CreateNewDependencyItemDialog<Customers, CustomersListView>("Новые третьи лица", out selectedItem);
            ThirdParties = item;
            SelectedThirdParties = selectedItem;
        }

        private void SelectDefendantsDialog(object obj)
        {
            Customers selectedItem;
            var item = CreateNewDependencyItemDialog<Customers, CustomersListView>("Новый ответчик", out selectedItem);
            Defendants = item;
            SelectedDefendant = selectedItem;
        }

        private void SelectPlaintiffsDialog(object obj)
        {
            Customers selectedItem;
            var item = CreateNewDependencyItemDialog<Customers, CustomersListView>("Новый истец", out selectedItem);
            Plaintiffs = item;
            SelectedPlaintiff = selectedItem;
        }

        private void SelectDistrictsDialog(object obj)
        {
            District selectedItem;
            var item = CreateNewDependencyItemDialog<District, DistrictsListView>("Новый федеральный округ", out selectedItem);
            Districts = item;
            SelectedDistrict = selectedItem;
        }

        private void SelectCourtTypesDialog(object obj)
        {
            CourtType selectedItem;
            var item = CreateNewDependencyItemDialog<CourtType, CourtTypesListView>("Новый типа иска", out selectedItem);
            CourtTypes = item;
            SelectedCourtType = selectedItem;
        }

        public DateTime? SelectedHearingDate
        {
            get { return Model.HearingDate; }
            set
            {
                Model.HearingDate = value;
                RaisePropertyChanged(() => SelectedHearingDate);
            }
        }
        public DateTime? SelectedDateOfDecision
        {
            get { return Model.DateOfDecision; }
            set
            {
                Model.DateOfDecision = value;
                RaisePropertyChanged(() => SelectedDateOfDecision);
            }
        }

        public Outcome SelectedOutcome
        {
            get { return Model.Outcome; }
            set
            {
                Model.Outcome = value;
                RaisePropertyChanged(() => SelectedOutcome);
            }
        }

        public IEnumerable<Outcome> Outcomes
        {
            get
            {
                var roles = Enum.GetValues(typeof(Outcome)).Cast<Outcome>();
                SelectedOutcome = Outcome.НетРешения;
                return roles;
            }
        }

        private ObservableCollection<StatementOfClaimType> _statementOfClaimTypes;
        public ObservableCollection<StatementOfClaimType> StatementOfClaimTypes
        {
            get { return _statementOfClaimTypes; }
            set
            {
                _statementOfClaimTypes = value;
                if (Model.StatementOfClaimType != null)
                {
                    SelectedStatementOfClaimType = StatementOfClaimTypes.First(x => x.Id == Model.StatementOfClaimType.Id);
                }
                RaisePropertyChanged(() => StatementOfClaimTypes);
            }
        }

        public StatementOfClaimType SelectedStatementOfClaimType
        {
            get { return Model.StatementOfClaimType; }
            set
            {
                Model.StatementOfClaimType = value;
                RaisePropertyChanged(() => SelectedStatementOfClaimType);
            }
        }
        
        private ObservableCollection<Court> _locationCaseMaterials;
        public ObservableCollection<Court> LocationCaseMaterials
        {
            get { return _locationCaseMaterials; }
            set
            {
                _locationCaseMaterials = value;
                if (Model.LocationCaseMaterials != null)
                {
                    SelectedLocationCaseMaterials = LocationCaseMaterials.First(x => x.Id == Model.LocationCaseMaterials.Id);
                }
                RaisePropertyChanged(() => LocationCaseMaterials);
            }
        }

        public Court SelectedLocationCaseMaterials
        {
            get { return Model.LocationCaseMaterials; }
            set
            {
                Model.LocationCaseMaterials = value;
                RaisePropertyChanged(() => SelectedLocationCaseMaterials);
            }
        }

        private ObservableCollection<Court> _courts;
        public ObservableCollection<Court> Courts
        {
            get { return _courts; }
            set
            {
                _courts = value;
                if (Model.Court != null)
                {
                    SelectedCourts = Courts.First(x => x.Id == Model.Court.Id);
                }
                RaisePropertyChanged(() => Courts);
            }
        }

        public Court SelectedCourts
        {
            get { return Model.Court; }
            set
            {
                Model.Court = value;
                RaisePropertyChanged(() => SelectedCourts);
            }
        }

        private ObservableCollection<Customers> _thirdParties;
        public ObservableCollection<Customers> ThirdParties
        {
            get { return _thirdParties; }
            set
            {
                _thirdParties = value;
                if (Model.ThirdParties != null)
                {
                    SelectedThirdParties = ThirdParties.First(x => x.Id == Model.ThirdParties.Id);
                }
                RaisePropertyChanged(() => ThirdParties);
            }
        }

        public Customers SelectedThirdParties
        {
            get { return Model.ThirdParties; }
            set
            {
                Model.ThirdParties = value;
                RaisePropertyChanged(() => SelectedThirdParties);
            }
        }

        private ObservableCollection<Customers> _sefendants;
        public ObservableCollection<Customers> Defendants
        {
            get { return _sefendants; }
            set
            {
                _sefendants = value;
                if (Model.Defendant != null)
                {
                    SelectedDefendant = Defendants.First(x => x.Id == Model.Defendant.Id);
                }
                RaisePropertyChanged(() => Defendants);
            }
        }

        public Customers SelectedDefendant
        {
            get { return Model.Defendant; }
            set
            {
                Model.Defendant = value;
                RaisePropertyChanged(() => SelectedDefendant);
            }
        }

        private ObservableCollection<Customers> _plaintiffs;
        public ObservableCollection<Customers> Plaintiffs
        {
            get { return _plaintiffs; }
            set
            {
                _plaintiffs = value;
                if (Model.Plaintiff != null)
                {
                    SelectedPlaintiff = Plaintiffs.First(x => x.Id == Model.Plaintiff.Id);
                }
                RaisePropertyChanged(() => Plaintiffs);
            }
        }

        public Customers SelectedPlaintiff
        {
            get { return Model.Plaintiff; }
            set
            {
                Model.Plaintiff = value;
                RaisePropertyChanged(() => SelectedPlaintiff);
            }
        }

        private ObservableCollection<CourtType> _courtTypes;
        public ObservableCollection<CourtType> CourtTypes
        {
            get { return _courtTypes; }
            set
            {
                _courtTypes = value;
                if (Model.CourtType != null)
                {
                    SelectedCourtType = CourtTypes.First(x => x.Id == Model.CourtType.Id);
                }
                RaisePropertyChanged(() => CourtTypes);
            }
        }

        public CourtType SelectedCourtType
        {
            get { return Model.CourtType; }
            set
            {
                Model.CourtType = value;
                RaisePropertyChanged(() => SelectedCourtType);
            }
        }

        private ObservableCollection<District> _districts;
        public ObservableCollection<District> Districts
        {
            get { return _districts; }
            set
            {
                _districts = value;
                if (Model.District != null)
                {
                    SelectedDistrict = Districts.First(x => x.Id == Model.District.Id);
                }
                RaisePropertyChanged(() => Districts);
            }
        }

        public District SelectedDistrict
        {
            get { return Model.District; }
            set
            {
                Model.District = value;
                RaisePropertyChanged(() => SelectedDistrict);
            }
        }

        public string Number {
            get { return Model.Number; }
            set
            {
                Model.Number = value;
                RaisePropertyChanged(() => Number);
            }
        }

        public override string Error
        {
            get { return this["Number"]; }
        }

        public override string this[string columnName]
        {
            get
            {
                string result = string.Empty;
                switch (columnName)
                {
                    case "Number":
                        if (string.IsNullOrWhiteSpace(Model.Number)) result = "Поле 'Название аппеляции' обязательное";
                        break;
                }
                return result;
            }
        }
    }
}
